package org.fujene.sandbox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

import junit.framework.TestCase;

public class TestThread extends TestCase {
	public static void testThread() throws InterruptedException, IOException {
		TestThreadRunnable runnable = new TestThreadRunnable();
		Thread th = new Thread(runnable);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		th.start();
		
		do {
//			System.out.print("Add for ? ->");
			String str = br.readLine();
			runnable.addList(Integer.parseInt(str));
		}while (th.isAlive());
	}

	public static class TestThreadRunnable implements Runnable {
		private int count = 0;
		private LinkedList<Integer> countList = new LinkedList<Integer>();
		
		public void addList(int newCount){
			countList.offer(Integer.valueOf(newCount));
		}
		
		@Override
		public void run() {
			int left = 10;
			int lastnum = 0;
			do {
				if(!countList.isEmpty()){
					while(!countList.isEmpty()){
						lastnum = countList.poll().intValue();
						count += lastnum;
						
						System.out.println("Count: " + Integer.toString(count));
						left--;
					}
				}
				try {
					Thread.sleep(25);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}while (left > 0);
		}
	}
}
